package myself.a014Recursion;

import java.util.Stack;


/**
 * @author 叶之越
 * Description
 * Date 2021/2/2
 * Time 14:11
 * Mail 739153436@qq.com
 */
public class ReverseStackUsingRecursive {
    // 反转栈，不使用其他数据结构
    public static void reverse(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        int cache = f(stack);
        reverse(stack);
        stack.push(cache);
    }

    // 弹出栈底元素
    private static int f(Stack<Integer> stack) {
        Integer cur = stack.pop();
        if (stack.isEmpty()) {
            return cur;
        }
        int result = f(stack);
        stack.push(cur);
        return result;
    }

}
